package com.chujian.Activities;

import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.chujian.Adapter.OrderCurrentListAdapter;
import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.model.CurrentOrderBean;
import com.chujian.model.Order;
import com.chujian.orderingsystem.R;

public class OrderCurrentListActivity extends Activity implements OnItemClickListener{
	private ListView mListView;
	private OrderCurrentListAdapter mAdapter;
//	private List<CurrentOrderBean> mList;
	private List<Order> mList;
	private Handler mHandler=new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			if(str!=null){
				Log.i("OrderCurrentListActivity", str);
				mList=JsonUtil.OrderListFromJson(str);
				mAdapter=new OrderCurrentListAdapter(OrderCurrentListActivity.this,mList);
				mListView.setAdapter(mAdapter);
				
			}
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_current_list);
		mListView=(ListView) findViewById(R.id.lv_order_current_list);
		mListView.setOnItemClickListener(this);
		
		String url=Constan.BASE_URL+"OrderListServlet";
		new HttpClientPostThread(url, mHandler, "0").start();;
	}
	/**
	 * 返回按钮监听
	 * @param v
	 */
	public void IbBack(View v){
		OrderCurrentListActivity.this.finish();
	}
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==1000&&resultCode==1001){
			int deletePosition=data.getIntExtra("position", -1);
			if(deletePosition!=-1){
				mList.remove(deletePosition);
				mAdapter.notifyDataSetChanged();
			}
		}
	};
	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Bundle bundle=new Bundle();
		bundle.putSerializable("currentOrder", mList.get(position));
		Intent intent=new Intent(OrderCurrentListActivity.this, OrderCurrentDetailSellerActivity.class);
		intent.putExtras(bundle);
		intent.putExtra("position", position);
		startActivityForResult(intent, 1000);
		
	}
}
